﻿@inject HttpClient _httpClient;
@inject IMessageService _message
@inject NavigationManager _NavigationManager

@inject IMessageService _message
@using EmployeeLeaveSystem.Shared.Models
@using System.Net.Http.Headers;

<!--文件上传指南-->
<Form Model="usr" OnFinish="OnUpdate" OnFinishFailed="(e)=>{OnUpdate(e);}">
    <FormItem Label="头像">
        <Avatar Shape="@AvatarShape.Square" Src="@("ImgsReq/"+Avatar_Name)" Size="150" />
        <InputFile OnChange="@LoadFiles" />
    </FormItem>
    <FormItem Label="姓名">
        <Input @bind-Value=context.Name />
    </FormItem>
    <FormItem Label="描述">
        <Input @bind-Value=context.Description />
    </FormItem>
    <FormItem Label="身份证">
        <Input @bind-Value=context.ID_Card />
    </FormItem>
    <FormItem Label="性别">
        <EnumSelect @bind-Value=context.StaffSex />
    </FormItem>
    <FormItem Label="电话">
        <Input @bind-Value=context.Phone_Number />
    </FormItem>
    <FormItem Label="政治面貌">
        <Input @bind-Value=context.Political_Status />
    </FormItem>
    <FormItem>
        <Button Block Type="@ButtonType.Primary" HtmlType="submit">递交</Button>
        </FormItem>
        </Form>
        @code {
    [Parameter]
    public int Id { get; set; }
    public String Avatar_Name = String.Empty;
    StaffInfo usr = new StaffInfo();
    protected override async Task OnParametersSetAsync() {
        usr = await _httpClient.GetFromJsonAsync<StaffInfo>($"api/Auth/GetUsrInfoById?id={Id}");
        Avatar_Name = usr.Avatar_Name;
        await base.OnParametersSetAsync();
    }

    private static bool FileUpload(IList<UploadResult> uploadResults,
    string? fileName, ILogger<FileUpload2> logger, out UploadResult result) {
        result = uploadResults.SingleOrDefault(f => f.FileName == fileName) ?? new();

        if (!result.Uploaded) {
            logger.LogInformation("{FileName} not uploaded (Err: 5)", fileName);
            result.ErrorCode = 5;
        }
        return result.Uploaded;
    }
    private async void LoadFiles(InputFileChangeEventArgs e) {
        var file = e.GetMultipleFiles(1).FirstOrDefault();
        Console.WriteLine("上传的文件名：" + file.Name);

        using var content = new MultipartFormDataContent(); // FormData

        var fileContent = new StreamContent(file.OpenReadStream()); // 打开文件进入读取

        fileContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType); // 编写文件类型

        content.Add(content: fileContent,
                    name: "\"files\"",
                    fileName: file.Name); // 加入content中MultipartFormDataContent

        var response = await _httpClient.PostAsync(@"api/File/UpLoadFile", content);
        var resultStr = await response.Content.ReadAsStringAsync();
        Console.WriteLine("服务器返回的文件名：" + resultStr);
        Avatar_Name = resultStr;
        InvokeAsync(StateHasChanged);
    }
    private async void OnUpdate(EditContext editContext) {
        _message.Info("开始更新");
        usr.Department = null;
        usr.Avatar_Name = Avatar_Name;
        await _httpClient.PostAsJsonAsync<StaffInfo>($"api/Auth/UpdateUsrInfo", usr);
        _message.Success("更新信息成功");
    }
}
<style>
    .avatar-uploader > .ant-upload {
        width: 128px;
        height: 128px;
    }
</style>